package president;

import home.HomePage;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Accountability;
import controllers.BKPayroll;
import controllers.Registered;

import secretary.AddStudentAccount;

public class ManageClearance extends WebPage{
	
	public ManageClearance () throws IOException, SQLException
	{
		Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		final SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		
		add(new BookmarkablePageLink("home", PresidentHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("first", ClearancePageFirst.class));
		add(new BookmarkablePageLink("second", ClearancePageSecond.class));
		add(new BookmarkablePageLink("third", ClearancePageThird.class));
		add(new BookmarkablePageLink("fourth", ClearancePageFourth.class));
		
		
		
		Form form = new Form("AccountabilityListForm");
		
		
		
		List<Registered> first = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 1);
		
		List fir = new ArrayList();
		Registered one = null;
		
	    for (Registered b : first)
	    {
	    	String id = b.getStudent_id();
	    	Registered reg = (Registered)sqlMap.queryForObject("Registered.getName", id);
	    	List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.isAccountable", id);
	    	
	    	if(acc.size() == 0)
	    	{
	    		final List ids = new ArrayList();
    			ids.add(b.getStudent_id());
	    	}
	    	else
	    	{
	    		fir.add(b.getStudent_id() + "\t" + reg.getStudent_name());
	    	}
	    }
		
	    ListView firstYear = new ListView("firstYear", fir) {
	        protected void populateItem(ListItem item) {
	            item.add(new Label("id", item.getModel()));
	        }
	    };
		
		

		List<Registered> second = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 2);
		
		List sec = new ArrayList();
		Registered two = null;
		
	    for (Registered c : second)
	    {
	    	String id = c.getStudent_id();
	    	Registered reg = (Registered)sqlMap.queryForObject("Registered.getName", id);
	    	List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.isAccountable", id);
	    	System.out.println(acc.size());
	    	if(acc.size() == 0)
	    	{
	    		final List ids = new ArrayList();
    			ids.add(c.getStudent_id());
	    	}
	    	else
	    	{
	    		sec.add(c.getStudent_id() + "\t" + reg.getStudent_name());
	    		System.out.println(c.getStudent_id());
	    	}
	    }
		
	    ListView secondYear = new ListView("secondYear", sec) {
	        protected void populateItem(ListItem item) {
	            item.add(new Label("id", item.getModel()));
	        }
	    };
		
		List<Registered> third = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 3);
		
		List th = new ArrayList();
		Registered three = null;
		
		for (Registered d : third)
	    {
	    	String id = d.getStudent_id();
	    	Registered reg = (Registered)sqlMap.queryForObject("Registered.getName", id);
	    	List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.isAccountable", id);
	    	
	    	if(acc.size() == 0)
	    	{
	    		final List ids = new ArrayList();
    			ids.add(d.getStudent_id());
	    	}
	    	else
	    	{
	    		th.add(d.getStudent_id() + "\t" + reg.getStudent_name());
	    	}
	    }
		
		ListView thirdYear = new ListView("thirdYear", th) {
	        protected void populateItem(ListItem item) {
	            item.add(new Label("id", item.getModel()));
	            
	        }
	    };
			
		List<Registered> fourth = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 4);
		
		List fth = new ArrayList();
		Registered four = null;
		
		for (Registered e : fourth)
	    {
	    	String id = e.getStudent_id();
	    	Registered reg = (Registered)sqlMap.queryForObject("Registered.getName", id);
	    	List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.isAccountable", id);
	    	
	    	if(acc.size() == 0)
	    	{
	    		final List ids = new ArrayList();
    			ids.add(e.getStudent_id());
	    	}
	    	else
	    	{
	    		fth.add(e.getStudent_id() + "\t" + reg.getStudent_name());
	    	}
	    }
		
		ListView fourthYear = new ListView("fourthYear", fth) {
	        protected void populateItem(ListItem item) {
	            item.add(new Label("id", item.getModel()));
	        }
	    };
		
		
			form.add(firstYear);
			form.add(secondYear);
			form.add(thirdYear);
			form.add(fourthYear);
			
			add(form);
	}

}
